﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRule;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using Newtonsoft.Json.Linq;
using ProjectManApi.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManApi.Controllers.xmgl
{
    public class ProFuKuanController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public ProFuKuanController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }

        #region 付款计划

        #region 导出

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ExportFKJHList(dynamic expjson)
        {

            JObject job = (JObject)expjson;

            List<string> ids = new List<string>();
            List<ColumnModel> cols = new List<ColumnModel>();

            if (null != job["ids"])
                ids = job["ids"].ToObject<string[]>().ToList();

            if (null != job["cols"])
                cols = job["cols"].ToObject<ColumnModel[]>().ToList();

            return File(await CProTZJHRule.GetExportList(ids, cols, BaseEditPower.TableName.付款月计划, "月付款计划"), "application/ms-excel", "投资计划列表.xls");
        }

        #endregion

        /// <summary>
        /// 新增付款计划列表
        /// </summary>
        /// <param name="htid"></param>
        /// <param name="bytb"></param>
        /// <param name="dxmc"></param>
        /// <param name="xmmc"></param>
        /// <param name="status"></param>
        /// <param name="current"></param>
        /// <param name="size"></param>
        /// <param name="sort"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetAddHTFKYJHIndex(string htid, string dxmc, string xmmc, string htbh, string htmc, string bytb, string bywtb, string status, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                //Dictionary<string, string> dics = new Dictionary<string, string>();
                //dics.Add("dxmc", dxmc);
                //dics.Add("htid", htid);
                //dics.Add("xmmc", xmmc);
                //dics.Add("status", status);
                ////本月填报
                //dics.Add("bytb", bytb);

                //dics.Add("current", current);
                //dics.Add("size", size);
                //dics.Add("sort", sort);
                //dics.Add("column", column);

                dynamic fkyjh = new ExpandoObject();
                fkyjh.dxmc = dxmc;
                fkyjh.htbh = htbh;
                fkyjh.htid = htid;
                fkyjh.xmmc = xmmc;
                fkyjh.bytb = bytb;
                fkyjh.bywtb = bywtb;


                fkyjh.htmc = htmc;

                fkyjh.status = status;
                fkyjh.current = current;
                fkyjh.size = size;
                fkyjh.sort = sort;
                fkyjh.column = column;

                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanYJHRule.GetAddHTFKYJHIndex(fkyjh, await GetDepId());//GetUserId()

                var dts = SelectTable.Item1;

                dts.ToList().ForEach(d =>
                {
                    string strtid = d.id;
                    string strsql = @" with t as
(select number-1 rn from master..spt_values where type = 'p')
select yf,(case when ISNULL(id, 'wtb') = 'wtb'
then 'wtb' else 'tb' end
)sftb from(select top 6 convert(varchar(6), dateadd(month, 0 - rn, GETDATE()), 112)yf from t
)tt
left join 
(select MAX(id)id,htid,REPLACE(months,'-','')months from CProFKYJH group by htid,months)CProFKYJH 
on tt.yf = CProFKYJH.months and CProFKYJH.htid = '" + strtid + "' order by yf asc";
                    DataTable dtsmx = TranstionRule.ProcessSql(strsql).Result;
                    List<dynamic> list = new List<dynamic>();
                    for (int i = 0; i < dtsmx.Rows.Count; i++)
                    {
                        dynamic dd = new ExpandoObject();
                        dd.yf = dtsmx.Rows[i]["yf"].ToString();
                        dd.sftb = dtsmx.Rows[i]["sftb"].ToString() == "tb" ? true : false;

                        list.Add(dd);
                    }

                    d.sftb = list;// dtsmx;

                });


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;

                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }


        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetHTFKYJHIndex(string formCode, string htmc, string month, string dxmc, string fkxmc, string xmmc, string status, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                //Dictionary<string, string> dics = new Dictionary<string, string>();
                //dics.Add("month", month);
                //dics.Add("dxmc", dxmc);
                //dics.Add("fkxmc", fkxmc);
                //dics.Add("xmmc", xmmc);

                //dics.Add("status", status);
                //dics.Add("current", current);
                //dics.Add("size", size);
                //dics.Add("sort", sort);
                //dics.Add("column", column);

                dynamic fkyjh = new ExpandoObject();
                fkyjh.month = month;
                fkyjh.dxmc = dxmc;
                fkyjh.fkxmc = fkxmc;
                fkyjh.xmmc = xmmc;
                fkyjh.htmc = htmc;

                fkyjh.status = status;
                fkyjh.current = current;
                fkyjh.size = size;
                fkyjh.sort = sort;
                fkyjh.column = column;

                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanYJHRule.GetHTFKYJHIndex(fkyjh, await GetDepId());//GetUserId()


                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;

                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetXZHTFKYJHIndex(string htmc, string month, string dxmc, string fkxmc, string xmmc, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                dynamic fkyjh = new ExpandoObject();
                fkyjh.month = month;
                fkyjh.dxmc = dxmc;
                fkyjh.fkxmc = fkxmc;
                fkyjh.xmmc = xmmc;
                fkyjh.htmc = htmc;


                string status = "checked";

                fkyjh.status = status;
                fkyjh.current = current;
                fkyjh.size = size;
                fkyjh.sort = sort;
                fkyjh.column = column;

                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanYJHRule.GetHTFKYJHIndex(fkyjh, await GetDepId());//GetUserId()


                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    list.Add(id);
                }


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;

                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTFKYJH(string htid)
        {
            try
            {
                var SelectTable = await ProFuKuanYJHRule.GetHTFKYJH(htid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTFKYJHByid(string id)
        {
            try
            {
                var SelectTable = await ProFuKuanYJHRule.GetHTFKYJHByid(id);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 修改月付款计划
        /// </summary>
        /// <param name="fkyjhjson"></param>
        /// <param name="strlx"></param>
        /// <returns></returns>
        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProConFKYJH(dynamic fkyjhjson)//, string strlx = "xxx"
        {

            string strope = "add";
            CProFKYJH fkyjh = new CProFKYJH();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)fkyjhjson;

                //获取方法
                strope = fkyjhjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                fkyjh = job["fkyjh"].ToObject<CProFKYJH>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "approved" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(fkyjh.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProFKYJH jhe = await TranstionRule.FindEntityByKey<CProFKYJH>(fkyjh.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    fkyjh.status = BaseEditPower.GetSPEnumValue("待审核");
                }

                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(fkyjh.htid))
                    {
                        return Fail("月付款计划关联的合同id不能为空");
                    }
                    if (fkyjh.jhje == 0)
                    {
                        return Fail("计划金额不能为空或零");
                    }

                    //重新赋值一个新id
                    fkyjh.id = CommonHelper.GetUniqueId().ToString();
                    fkyjh.createuser = GetUserId();

                    //jhfkbm
                    fkyjh.jhfkbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.付款月计划.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(fkyjhjson), await GetICApiInfo());
                    //await BusinessRule.Assets.CreateZCDM("XM", "YFKJH");

                    fkyjh.sqr = GetUserName();
                    fkyjh.sqrq = DateTime.Now;

                    if (strlsope != "submit")
                        fkyjh.status = BaseEditPower.GetSPEnumValue("待提交");
                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fkyjh.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.付款月计划.ToString(), fkyjh.id, fkyjh.htid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = fkyjh.jhfkbm, amount = fkyjh.jhje?.ToString(), content = "[月付款计划]" + fkyjh.months + "月" + fkyjh.jhfkx, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.付款月计划.ToString(), fkyjh.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(fkyjhjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        //if (!bsp)
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        fkyjh.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    //新增投资计划
                    await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "add");


                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(fkyjh.id))
                    {
                        return Fail("需要修改的月付款计划id不能为空");
                    }


                    if (string.IsNullOrEmpty(fkyjh.htid))
                    {
                        //CProFKYJH jhe = await TranstionRule.FindEntityByKey<CProFKYJH>(fkyjh.id);
                        fkyjh.htid = null;
                    }

                    //if (string.IsNullOrEmpty(fkyjh.htid))
                    //{
                    //    return Fail("变更关联的合同id不能为空");
                    //}
                    //if (fkyjh.jhje == 0)
                    //{
                    //    return Fail("计划金额不能为空或零");
                    //}

                    if (!await BaseEditPower.GetEditPower(fkyjh.id, BaseEditPower.TableName.付款月计划))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fkyjh.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        CProFKYJH jhe = await TranstionRule.FindEntityByKey<CProFKYJH>(fkyjh.id);
                        if (null == jhe)
                        {
                            return Fail("根据id未检索到付款月计划信息");
                        }

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.付款月计划.ToString(), fkyjh.id, fkyjh.htid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = jhe.jhfkbm, amount = fkyjh.jhje?.ToString(), content = "[月付款计划]" + fkyjh.months + "月" + fkyjh.jhfkx, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.付款月计划.ToString(), fkyjh.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(fkyjhjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        //if (!bsp)
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        fkyjh.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    //新增投资计划
                    await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "edit");
                }
                else if (strope == "approved")
                {
                    if (string.IsNullOrEmpty(fkyjh.id))
                    {
                        return Fail("合同变更编号不能为空");
                    }
                    if (fkyjh.shje == 0)
                    {
                        return Fail("变更审定金额不能为空或零");
                    }

                    //获取概算的当前状态
                    CProFKYJH yfkjh = await TranstionRule.FindEntityByKey<CProFKYJH>(fkyjh.id);
                    string strstatus = "";
                    if (null != yfkjh)
                    {
                        strstatus = yfkjh.status ?? "";
                    }

                    if (strstatus == "2")
                    {
                        //这里先绑定附件
                        await JavaApi.UploadFileJavaAPI(fkyjh.id, filecodes, await GetICApiInfo());


                        //新增投资计划
                        await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "edit");
                    }
                    else
                    {
                        return Fail("数据状态为" + strstatus + ",未审核完成");
                    }
                }


                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        ///// <summary>
        ///// 修改月付款计划
        ///// </summary>
        ///// <param name="fkyjh"></param>
        ///// <param name="strlx"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertCProConFKYJH(CProFKYJH fkyjh, string strlx = "xxx")//
        //{
        //    try
        //    {
        //        string strope = strlx.ToLower();
        //        if (strope != "add" && strope != "edit" && strope != "approved")
        //        {
        //            return Fail("strlx参数错误");
        //        }

        //        if (strope == "add")
        //        {
        //            if (string.IsNullOrEmpty(fkyjh.htid))
        //            {
        //                return Fail("月付款计划关联的合同id不能为空");
        //            }
        //            if (fkyjh.jhje == 0)
        //            {
        //                return Fail("计划金额不能为空或零");
        //            }

        //            //重新赋值一个新id
        //            fkyjh.id = CommonHelper.GetUniqueId().ToString();
        //            fkyjh.createuser = GetUserId();

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "add");


        //        }
        //        else if (strope == "edit")
        //        {
        //            if (string.IsNullOrEmpty(fkyjh.id))
        //            {
        //                return Fail("需要修改的月付款计划id不能为空");
        //            }
        //            //if (string.IsNullOrEmpty(fkyjh.htid))
        //            //{
        //            //    return Fail("变更关联的合同id不能为空");
        //            //}
        //            //if (fkyjh.jhje == 0)
        //            //{
        //            //    return Fail("计划金额不能为空或零");
        //            //}

        //            if (!await BaseEditPower.GetEditPower(fkyjh.id, BaseEditPower.TableName.付款月计划))
        //            {
        //                return Fail("响应失败:请确认数据权限");
        //            }

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "edit");
        //        }
        //        else if (strope == "approved")
        //        {
        //            if (string.IsNullOrEmpty(fkyjh.id))
        //            {
        //                return Fail("合同变更编号不能为空");
        //            }
        //            if (fkyjh.shje == 0)
        //            {
        //                return Fail("变更审定金额不能为空或零");
        //            }

        //            //获取概算的当前状态
        //            CProFKYJH yfkjh = await TranstionRule.FindEntity<CProFKYJH>(fkyjh.id);
        //            string strstatus = "";
        //            if (null != yfkjh)
        //            {
        //                strstatus = yfkjh.status ?? "";
        //            }

        //            if (strstatus == "2")
        //            {
        //                //新增投资计划
        //                await ProFuKuanYJHRule.InsertHTYJH(fkyjh, "edit");
        //            }
        //            else
        //            {
        //                return Fail("数据状态为" + strstatus + ",未审核完成");
        //            }
        //        }


        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail("响应失败:" + ex.Message);
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProConFKYJHMulti(Root ids)//
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (string.IsNullOrEmpty(idm.id))
                    {
                        return Fail("需要删除的付款计划编号不能为空");
                    }

                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.付款月计划))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }
                }
            }
            catch (Exception ex)
            {
                return Fail("出错：" + ex.Message);
            }


            try
            {
                //新增投资计划
                if (await ProFuKuanYJHRule.DeleteHTFKYJH(ids))
                {
                    return Success("成功");
                }
                else
                {
                    return Fail("影响行数为0");
                }

            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        #endregion

        #region 付款申请

        #region 导出

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ExportFKSQList(dynamic expjson)
        {

            JObject job = (JObject)expjson;

            List<string> ids = new List<string>();
            List<ColumnModel> cols = new List<ColumnModel>();

            if (null != job["ids"])
                ids = job["ids"].ToObject<string[]>().ToList();

            if (null != job["cols"])
                cols = job["cols"].ToObject<ColumnModel[]>().ToList();

            return File(await CProTZJHRule.GetExportList(ids, cols, BaseEditPower.TableName.付款申请, "付款申请列表"), "application/ms-excel", "投资计划列表.xls");
        }

        #endregion

        //待审批
        [HttpGet]
        public async Task<IActionResult> GetFuKuanSQListDB([FromQuery] ProFuKuanSQQuerry querry)
        {
            try
            {
                List<JavaApi.SPSDataItem> spitems1 = await JavaApi.GetSPDBJavaAPI(querry.formCode, GetUserAccount(), querry.audittype, querry.current, querry.size, await GetICApiInfo());
                string businessIds = "";

                for (int i = 0; i < spitems1.Count; i++)
                {
                    businessIds += "'" + spitems1[i].businessId + "',";
                }

                businessIds = businessIds.TrimEnd(',');

                if (!string.IsNullOrEmpty(businessIds))
                {
                    businessIds = " and id in (" + businessIds + ")";
                }
                else//如果没有数据 就返回空
                {
                    Page pa1 = new Page();
                    pa1.records = null;
                    pa1.total = 0;
                    return Success(pa1);
                }


                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanRule.GetFuKuanSQListDB(querry, await GetDepId(), businessIds);

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        //这个是列表
        [HttpGet]
        //string formCode,string xmid ,string htmc,string xmmc, string htid, string htbh, string dxmc, string fkmc, string jhfk, string status, DateTime? begindate, DateTime? enddate, string current = "1", string size = "20", string sort = "desc", string column = "id"
        public async Task<IActionResult> GetFuKuanSQList([FromQuery] ProFuKuanSQQuerry querry)
        {
            try
            {
                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanRule.GetFuKuanSQList(querry, await GetDepId());

                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetHTFuKuanSQList(string htid, string current = "1", string size = "2000", string sort = "desc", string column = "id")
        {
            try
            {
                ProFuKuanSQQuerry querry = new ProFuKuanSQQuerry();
                //基础属性
                querry.size = CommonHelper.ParseInt(size);
                querry.current = CommonHelper.ParseInt(current);
                querry.sort = sort;
                querry.column = column;


                querry.htid = htid;

                (IEnumerable<dynamic>, int) SelectTable = await ProFuKuanRule.GetHTFuKuanSQList(querry, await GetDepId());


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 查询合同下付款申请
        /// </summary>
        /// <param name="htid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTFKSQ(string htid)
        {
            try
            {
                var SelectTable = await ProFuKuanYJHRule.GetHTFKSQ(htid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 查询单条付款申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetHTFKSQByid(string id)
        {
            try
            {
                var SelectTable = await ProFuKuanYJHRule.GetHTFKSQByid(id);

                SelectTable.zfmxs = await ProFuKuanYJHRule.GetFKMXBysqid(id);

                //dynamic HTFKYJH = new ExpandoObject();

                //if (null != SelectTable)
                //{
                //    string strjhid = SelectTable.jhid;

                //    HTFKYJH = await ProFuKuanYJHRule.GetHTFKYJHByid(strjhid);

                //    SelectTable.fkjh = HTFKYJH;
                //}


                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 新增修改付款申请
        /// </summary>
        /// <param name="fksq"></param>
        /// <param name="strlx"></param>
        /// <returns></returns>
        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProConFKSQ(dynamic fksqjson)//
        {
            string strope = "add";
            CProFKSQ fksq = new CProFKSQ();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)fksqjson;

                //获取方法
                strope = fksqjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                fksq = job["fksq"].ToObject<CProFKSQ>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "approved" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(fksq.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProFKSQ jhe = await TranstionRule.FindEntityByKey<CProFKSQ>(fksq.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    fksq.status = BaseEditPower.GetSPEnumValue("待审核");
                }


                string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.月付款计划.ToString());
                string strlxsqje = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.申请金额限制.ToString());

                if (strope == "add")
                {
                    //重新赋值一个新id
                    fksq.id = CommonHelper.GetUniqueId().ToString();
                    fksq.createuser = GetUserId();


                    if (string.IsNullOrEmpty(fksq.htid))
                    {
                        return Fail("付款申请合同主键htid不能为空");
                    }
                    if (fksq.sqje == 0)
                    {
                        return Fail("付款申请申请金额不能为空或零");
                    }
                    if (string.IsNullOrEmpty(fksq.skyh))
                    {
                        return Fail("付款申请申请收款银行不能为空");
                    }
                    if (string.IsNullOrEmpty(fksq.skzh))
                    {
                        return Fail("付款申请申请收款账号不能为空");
                    }

                    if (strlxsqje == "1")
                    {
                        if (strlx == "1")
                        {
                            if (string.IsNullOrEmpty(fksq.jhid))
                            {
                                return Fail("付款申请计划编号jhid不能为空");
                            }

                            DataTable dataTable = await ProFuKuanYJHRule.GetSHJEXZ(fksq.jhid, fksq.id);
                            for (int i = 0; i < dataTable.Rows.Count; i++)
                            {
                                decimal dksqje = CommonHelper.ParseDecimal(dataTable.Rows[i]["ksqje"].ToString());
                                if (dksqje < fksq.sqje)
                                {
                                    return Fail("超出月付款计划金额");
                                }
                            }
                        }
                        else
                        {
                            //if (string.IsNullOrEmpty(fksq.htid))
                            //{
                            //    return Fail("付款申请计划编号jhid不能为空");
                            //}
                            DataTable dataTable = await ProFuKuanYJHRule.GetSHJEXZ2(fksq.htid, fksq.id);
                            for (int i = 0; i < dataTable.Rows.Count; i++)
                            {
                                decimal dksqje = CommonHelper.ParseDecimal(dataTable.Rows[i]["ksqje"].ToString());
                                if (dksqje < fksq.sqje)
                                {
                                    return Fail("超出合同付款计划");
                                }
                            }
                        }
                    }




                    //付款申请
                    fksq.fkbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.付款申请.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(fksqjson), await GetICApiInfo());
                    //await BusinessRule.Assets.CreateZCDM("XM", "FKSQ");


                    if (strlsope != "submit")
                        fksq.status = BaseEditPower.GetSPEnumValue("待提交");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fksq.id, filecodes, await GetICApiInfo());
                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        string strsfgl = strlx == "1" ? "是" : "否";
                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.付款申请.ToString(), strsfgl, fksq.id, fksq.htid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = fksq.fkbm, amount = fksq.sqje?.ToString(), content = "[付款申请]" + fksq.fkmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.付款申请.ToString(), fksq.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(fksqjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        //if (!bsp)
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        fksq.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    //新增投资计划
                    await ProFuKuanYJHRule.InsertHTFKSQ(fksq, "add");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(fksq.id))
                    {
                        return Fail("付款申请id不能为空");
                    }


                    if (!await BaseEditPower.GetEditPower(fksq.id, BaseEditPower.TableName.付款申请))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fksq.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        CProFKSQ jhe = await TranstionRule.FindEntityByKey<CProFKSQ>(fksq.id);
                        if (null == jhe)
                        {
                            return Fail("根据id未检索到付款申请信息");
                        }


                        string strsfgl = strlx == "1" ? "是" : "否";
                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.付款申请.ToString(), strsfgl, fksq.id, fksq.htid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = jhe.fkbm, amount = fksq.sqje?.ToString(), content = "[付款申请]" + fksq.fkmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.付款申请.ToString(), fksq.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(fksqjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        //if (!bsp)
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        fksq.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }
                    //新增投资计划
                    await ProFuKuanYJHRule.InsertHTFKSQ(fksq, "edit");
                }


                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProConFKSQMulti(Root ids)//
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (string.IsNullOrEmpty(idm.id))
                    {
                        return Fail("付款申请id不能为空");
                    }

                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.付款申请))
                    {
                        //throw new Exception("请确认数据权限");
                        return Fail("响应失败:请确认数据权限");
                    }
                }
            }
            catch (Exception ex)
            {
                return Fail("出错：" + ex.Message);
            }


            try
            {
                //新增投资计划
                if (await ProFuKuanYJHRule.DeleteHTFKSQ(ids))
                {
                    return Success("成功");
                }
                else
                {
                    return Fail("影响行数为0");
                }

            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }


        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetFKSQKYJE(dynamic kyyejson)//
        {
            string yjhid = "";
            string htid = "";
            string sqid = "";
            string ope = "";

            try
            {
                yjhid = kyyejson.yjhid ?? "";
                htid = kyyejson.htid ?? "";
                sqid = kyyejson.sqid ?? "";
                ope = kyyejson.ope ?? "";

                if (ope != "add" && ope != "edit" && ope != "audit")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            if (string.IsNullOrEmpty(htid.Trim()))
            {
                return Fail("参数htid 不能为空");
            }
            if (ope == "edit")
            {
                //如果为空
                if (string.IsNullOrEmpty(sqid))
                {
                    return Fail("参数sqid 不能为空");
                }
            }

            string strwhereyjh = " and jhid = '" + yjhid + "'";
            if (string.IsNullOrEmpty(yjhid))
            {
                yjhid = "jh123456789987654321";
                strwhereyjh = "";
            }

            if (ope == "audit" || ope == "add")
            {
                sqid = "sq123456789987654321";
            }

            if (string.IsNullOrEmpty(sqid))
            {
                sqid = "sq123456789987654321";
            }

            try
            {
                string strsql = @"select id,htzje,ISNULL(sdje,0)sdje,ISNULL(yingfu,0)yingfu,sqzje,ISNULL(jhje,0)jhje,
ISNULL(jhje,0)-sqzje ksqyjh,
(case when ISNULL(yingfu, 0) = 0 then(
case when ISNULL(sdje, 0) = 0 then htzje else ISNULL(sdje, 0) end
) else ISNULL(yingfu, 0) end)yingfuht,
(case when ISNULL(yingfu,0) = 0 then(
case when ISNULL(sdje, 0) = 0 then htzje else ISNULL(sdje, 0) end
) else ISNULL(yingfu, 0) end)-sqzje ksqht
 from(
select id, htje + isnull((select sum(bgsdje) from dbo.CProConBG where htid = tht.id and status = '2'), 0) htzje,
(select sdje from CProJS where status = '2' and htid = tht.id)sdje,
(select yingfu from View_FKJHTOYFJE where id = tht.id)yingfu,
(select shje from CProFKYJH where htid = tht.id and id = '" + yjhid + @"')jhje,
isnull((select SUM(sqje) from CProFKSQ where htid = tht.id " + strwhereyjh + " and id <> '" + sqid + @"'),0)sqzje
from CProContract tht where id = '" + htid + @"'
)tt ";

                DataTable dts = await TranstionRule.ProcessSql(strsql);
                string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.月付款计划.ToString());


                Dictionary<string, object> dics = new Dictionary<string, object>();
                for (int i = 0; i < dts.Rows.Count; i++)
                {
                    //月计划
                    if (strlx == "1")
                    {
                        string strje = dts.Rows[i]["ksqyjh"].ToString();
                        string strjhje = dts.Rows[i]["jhje"].ToString();
                        string strsqzje = dts.Rows[i]["sqzje"].ToString();

                        dics.Add("kyje", strje);
                        dics.Add("xzlx", "yjh");
                        dics.Add("jhje", strjhje);
                        dics.Add("sqzje", strsqzje);
                    }
                    else
                    {
                        string strje = dts.Rows[i]["ksqht"].ToString();
                        string strjhje = dts.Rows[i]["yingfuht"].ToString();
                        string strsqzje = dts.Rows[i]["sqzje"].ToString();

                        dics.Add("kyje", strje);
                        dics.Add("xzlx", "htjh");
                        dics.Add("jhje", strjhje);
                        dics.Add("sqzje", strsqzje);

                    }

                    dics.Add("sm", "kyje 可申请金额！和 UI上申请金额比较 ");
                }

                return Success(dics);
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }

        }

        #endregion

        #region 支付
        /// <summary>
        /// 付款记录列表-根据sqid获取付款明细
        /// </summary>
        /// <param name="sqid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetFKMXBysqid(string sqid)
        {
            try
            {
                var SelectTable = await ProFuKuanRule.GetFKMXBysqid(sqid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        /// <summary>
        /// 付款明细-根据fkid获取单条付款明细
        /// </summary>
        /// <param name="zfid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetFKMXByfkid(string zfid)
        {
            try
            {
                var SelectTable = await ProFuKuanRule.GetFKMXByfkid(zfid);

                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertCProFKMXMul(CProFKMXCurd fkmxjson)//, string strlx = "xxx"
        {
            string strope = "add";
            List<CProFKMX> fkmx = new List<CProFKMX>();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                //JObject job = (JObject)fkmxjson;

                //获取方法
                strope = fkmxjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                //fkmx = job["fkmx"].ToObject<List<CProFKMX>>();

                if (null != fkmxjson.fkmx)
                {
                    fkmx = fkmxjson.fkmx;
                }

                //接收附件列表
                //if (null != job["apiargs"])
                //    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "approved")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (strope == "add")
                {
                    for (int i = 0; i < fkmx.Count; i++)
                    {
                        //重新赋值一个新id
                        fkmx[i].id = CommonHelper.GetUniqueId().ToString();
                        fkmx[i].createuser = GetUserName();

                        string id = fkmx[i].id;

                        string sqid = fkmx[i].sqid;
                        decimal? dzfje = fkmx[i].zfje;
                        if (string.IsNullOrEmpty(sqid))
                        {
                            return Fail("付款明细申请id不能为空");
                        }
                        if (dzfje == 0)
                        {
                            return Fail("付款明细支付金额不能为空或零");
                        }

                        CProFKSQ fksq = await TranstionRule.FindEntity<CProFKSQ>(new { id = sqid });
                        if (fksq.status != "2")
                        {
                            return Fail("付款申请未审核通过");
                        }

                        string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id and id <> '" + id + "'),0)zfje from CProFKSQ where id = '" + sqid + "'";

                        DataTable dts = await TranstionRule.ProcessSql(strsql);
                        decimal sqje = 0;
                        decimal zfje = 0;
                        decimal dd = 0;
                        for (int r = 0; r < dts.Rows.Count; r++)
                        {
                            sqje = dts.Rows[r]["sqje"].ToDecimal();
                            zfje = dts.Rows[r]["zfje"].ToDecimal();
                        }

                        if (sqje < zfje + dzfje)
                        {
                            return Fail("付款明细付款金额超申请金额");
                        }

                   
                    }


                    //这里先绑定附件
                    //await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

                    //新增投资计划
                    await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "add");


                }
                else if (strope == "edit")
                {
                    //if (string.IsNullOrEmpty(fkmx.id))
                    //{
                    //    return Fail("付款申请id不能为空");
                    //}

                    //string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id and id <> '" + fkmx.id + "'),0)zfje from CProFKSQ where id = '" + fkmx.sqid + "'";

                    //DataTable dts = await TranstionRule.ProcessSql(strsql);
                    //decimal sqje = 0;
                    //decimal zfje = 0;
                    //decimal dd = 0;
                    //for (int i = 0; i < dts.Rows.Count; i++)
                    //{
                    //    sqje = dts.Rows[i]["sqje"].ToDecimal();
                    //    zfje = dts.Rows[i]["zfje"].ToDecimal();
                    //}

                    //if (sqje < zfje + fkmx.zfje)
                    //{
                    //    return Fail("付款明细付款金额超申请金额");
                    //}

                    //这里先绑定附件
                    //await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

                    for (int i = 0; i < fkmx.Count; i++)
                    {
                        string sqid = fkmx[i].sqid;
                        decimal? dzfje = fkmx[i].zfje;

                        string id = fkmx[i].id;

                        if (string.IsNullOrEmpty(id))
                        {
                            return Fail("付款申请id不能为空");
                        }

                        string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id and id <> '" + id + "'),0)zfje from CProFKSQ where id = '" + sqid + "'";

                        DataTable dts = await TranstionRule.ProcessSql(strsql);
                        decimal sqje = 0;
                        decimal zfje = 0;
                        decimal dd = 0;
                        for (int r = 0; r < dts.Rows.Count; r++)
                        {
                            sqje = dts.Rows[r]["sqje"].ToDecimal();
                            zfje = dts.Rows[r]["zfje"].ToDecimal();
                        }

                        if (sqje < zfje + dzfje)
                        {
                            return Fail("付款明细付款金额超申请金额");
                        }

                        //重新赋值一个新id
                        fkmx[i].id = CommonHelper.GetUniqueId().ToString();
                        fkmx[i].createuser = GetUserName();
                    }

                    //新增投资计划
                    await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "edit");
                }


                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 插入修改
        /// </summary>
        /// <param name="fkmxjson"></param>
        /// <returns></returns>
        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProFKMX(dynamic fkmxjson)//, string strlx = "xxx"
        {

            string strope = "add";
            CProFKMX fkmx = new CProFKMX();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)fkmxjson;

                //获取方法
                strope = fkmxjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                fkmx = job["fkmx"].ToObject<CProFKMX>();

                //if (null != job["tzxmids"])
                //    tzxmids = job["tzxmids"].ToObject<List<CProManageTZXM>>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "approved")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(fkmx.sqid))
                    {
                        return Fail("付款明细申请id不能为空");
                    }
                    if (fkmx.zfje == 0)
                    {
                        return Fail("付款明细支付金额不能为空或零");
                    }
                    //if (string.IsNullOrEmpty(fkmx.fkyh))
                    //{
                    //    return Fail("付款明细付款银行不能为空");
                    //}
                    //if (string.IsNullOrEmpty(fkmx.fkzh))
                    //{
                    //    return Fail("付款明细付款账号不能为空");
                    //}

                    //重新赋值一个新id
                    fkmx.id = CommonHelper.GetUniqueId().ToString();
                    fkmx.createuser = GetUserId();


                    string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id and id <> '" + fkmx.id + "'),0)zfje from CProFKSQ where id = '" + fkmx.sqid + "'";

                    DataTable dts = await TranstionRule.ProcessSql(strsql);
                    decimal sqje = 0;
                    decimal zfje = 0;
                    decimal dd = 0;
                    for (int i = 0; i < dts.Rows.Count; i++)
                    {
                        sqje = dts.Rows[i]["sqje"].ToDecimal();
                        zfje = dts.Rows[i]["zfje"].ToDecimal();
                    }

                    if (sqje < zfje + fkmx.zfje)
                    {
                        return Fail("付款明细付款金额超申请金额");
                    }



                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

                    //新增投资计划
                    await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "add");


                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(fkmx.id))
                    {
                        return Fail("付款申请id不能为空");
                    }

                    string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id and id <> '" + fkmx.id + "'),0)zfje from CProFKSQ where id = '" + fkmx.sqid + "'";

                    DataTable dts = await TranstionRule.ProcessSql(strsql);
                    decimal sqje = 0;
                    decimal zfje = 0;
                    decimal dd = 0;
                    for (int i = 0; i < dts.Rows.Count; i++)
                    {
                        sqje = dts.Rows[i]["sqje"].ToDecimal();
                        zfje = dts.Rows[i]["zfje"].ToDecimal();
                    }

                    if (sqje < zfje + fkmx.zfje)
                    {
                        return Fail("付款明细付款金额超申请金额");
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

                    //新增投资计划
                    await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "edit");
                }


                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        ///// <summary>
        ///// 插入修改
        ///// </summary>
        ///// <param name="fkmxjson"></param>
        ///// <returns></returns>
        //[HttpPost]
        ////[AllowAnonymous]
        //public async Task<IActionResult> InsertCProFKMX(dynamic fkmxjson)//, string strlx = "xxx"
        //{

        //    string strope = "add";
        //    CProFKMX fkmx = new CProFKMX();


        //    //添加附件上传
        //    FileUploadent filecodes = new FileUploadent();
        //    try
        //    {
        //        JObject job = (JObject)fkmxjson;

        //        //获取方法
        //        strope = fkmxjson.ope ?? "";
        //        strope = strope.ToLower();

        //        //获取实体类 这样复杂类型不用定义实体类
        //        fkmx = job["fkmx"].ToObject<CProFKMX>();

        //        //if (null != job["tzxmids"])
        //        //    tzxmids = job["tzxmids"].ToObject<List<CProManageTZXM>>();

        //        //接收附件列表
        //        if (null != job["apiargs"])
        //            filecodes = job["apiargs"].ToObject<FileUploadent>();


        //        if (strope != "add" && strope != "edit" && strope != "approved")
        //        {
        //            return Fail("参数ope 不合法");
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail("参数转换出错：" + ex.Message);
        //    }

        //    try
        //    {
        //        //string strope = strlx.ToLower();
        //        //if (strope != "add" && strope != "edit")
        //        //{
        //        //    return Fail("strlx参数错误");
        //        //}

        //        if (strope == "add")
        //        {
        //            if (string.IsNullOrEmpty(fkmx.sqid))
        //            {
        //                return Fail("付款明细申请id不能为空");
        //            }
        //            if (fkmx.zfje == 0)
        //            {
        //                return Fail("付款明细支付金额不能为空或零");
        //            }
        //            if (string.IsNullOrEmpty(fkmx.fkyh))
        //            {
        //                return Fail("付款明细付款银行不能为空");
        //            }
        //            if (string.IsNullOrEmpty(fkmx.fkzh))
        //            {
        //                return Fail("付款明细付款账号不能为空");
        //            }


        //            string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id),0)zfje from CProFKSQ where id = '" + fkmx.sqid + "'";

        //            DataTable dts = await TranstionRule.ProcessSql(strsql);
        //            decimal sqje = 0;
        //            decimal zfje = 0;
        //            decimal dd = 0;
        //            for (int i = 0; i < dts.Rows.Count; i++)
        //            {
        //                sqje = dts.Rows[i]["sqje"].ToDecimal();
        //                zfje = dts.Rows[i]["zfje"].ToDecimal();
        //            }
        //            //IEnumerable<dynamic> dyn = await TranstionRule.ProcessSqld(strsql);
        //            //dyn.ToList().ForEach(d =>
        //            //{
        //            //    dd = d.sqje;
        //            //});

        //            //var cc = dyn.ToList().First();
        //            //dd = cc.sqje;

        //            if (sqje < zfje + fkmx.zfje)
        //            {
        //                return Fail("付款明细付款金额超申请金额");
        //            }

        //            //重新赋值一个新id
        //            fkmx.id = CommonHelper.GetUniqueId().ToString();
        //            fkmx.createuser = GetUserId();

        //            //这里先绑定附件
        //            await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "add");


        //        }
        //        else if (strope == "edit")
        //        {
        //            if (string.IsNullOrEmpty(fkmx.id))
        //            {
        //                return Fail("付款申请id不能为空");
        //            }

        //            //这里先绑定附件
        //            await JavaApi.UploadFileJavaAPI(fkmx.id, filecodes, await GetICApiInfo());

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "edit");
        //        }


        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail("响应失败:" + ex.Message);
        //    }
        //}

        ///// <summary>
        ///// 插入修改
        ///// </summary>
        ///// <param name="fkmx"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> InsertCProFKMX(CProFKMX fkmx, string strlx = "xxx")
        //{
        //    try
        //    {
        //        string strope = strlx.ToLower();
        //        if (strope != "add" && strope != "edit")
        //        {
        //            return Fail("strlx参数错误");
        //        }

        //        if (strope == "add")
        //        {
        //            if (string.IsNullOrEmpty(fkmx.sqid))
        //            {
        //                return Fail("付款明细申请id不能为空");
        //            }
        //            if (fkmx.zfje == 0)
        //            {
        //                return Fail("付款明细支付金额不能为空或零");
        //            }
        //            if (string.IsNullOrEmpty(fkmx.fkyh))
        //            {
        //                return Fail("付款明细付款银行不能为空");
        //            }
        //            if (string.IsNullOrEmpty(fkmx.fkzh))
        //            {
        //                return Fail("付款明细付款账号不能为空");
        //            }


        //            string strsql = "select sqje,isnull((select SUM(zfje) from CProFKMX where sqid = CProFKSQ.id),0)zfje from CProFKSQ where id = '" + fkmx.sqid + "'";

        //            DataTable dts = await TranstionRule.ProcessSql(strsql);
        //            decimal sqje = 0;
        //            decimal zfje = 0;
        //            decimal dd = 0;
        //            for (int i = 0; i < dts.Rows.Count; i++)
        //            {
        //                sqje = dts.Rows[i]["sqje"].ToDecimal();
        //                zfje = dts.Rows[i]["zfje"].ToDecimal();
        //            }
        //            //IEnumerable<dynamic> dyn = await TranstionRule.ProcessSqld(strsql);
        //            //dyn.ToList().ForEach(d =>
        //            //{
        //            //    dd = d.sqje;
        //            //});

        //            //var cc = dyn.ToList().First();
        //            //dd = cc.sqje;

        //            if (sqje < zfje + fkmx.zfje)
        //            {
        //                return Fail("付款明细付款金额超申请金额");
        //            }

        //            //重新赋值一个新id
        //            fkmx.id = CommonHelper.GetUniqueId().ToString();
        //            fkmx.createuser = GetUserId();

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "add");


        //        }
        //        else if (strope == "edit")
        //        {
        //            if (string.IsNullOrEmpty(fkmx.id))
        //            {
        //                return Fail("付款申请id不能为空");
        //            }

        //            //新增投资计划
        //            await ProFuKuanYJHRule.InsertCProFKMX(fkmx, "edit");
        //        }


        //        return Success("成功");
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail("响应失败:" + ex.Message);
        //    }
        //}

        /// <summary>
        /// 根据付款id删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProFKMXMulti(Root ids)
        {
            try
            {
                await ProFuKuanYJHRule.DeleteCProFKMXMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion



    }
}
